import {createRouter, createWebHistory} from 'vue-router'
import Home from '@/views/Home'
import ShoppingCart from '@/views/ShoppingCart'
import BooksNew from '@/components/BooksNew'
import Book from '@/views/Book'
import UserRegister from '@/components/UserRegister'
import UserLogin from '@/components/UserLogin'
import LineChart from "../components/LineChart";
import CeShi2 from "../views/CeShi2";
import CeShi1 from "../views/CeShi1";


const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes: [{
        path: '/',
        // redirect: {
        //     name: "home"
        // },
        redirect: '/home'
    },
        {
            path: '/home',
            name: 'home',
            meta: {
                title: '首页'
            },
            component: Home
        }, {
            path: "/category/:id",
            name: 'category',
            meta: {
                title: '图书分类'
            },
            // 延迟路由，进行webpack打包时，会将其划分成单独的js文件，在路由匹配到该组件时，会自动请求这个资源
            component: Book
        }, {
            path: "/hot/:id",
            name: 'hot',
            meta: {
                title: '热门推荐'
            },
            // 延迟路由，进行webpack打包时，会将其划分成单独的js文件，在路由匹配到该组件时，会自动请求这个资源
            component: Book
        },
        {
            path: "/search",
            name: 'search',
            meta: {
                title: '搜索结果'
            },
            component: () =>
                import ('@/views/Books.vue')
        },
        {
            path: "/cart",
            name: 'cart',
            meta: {
                title: '购物车'
            },
            component: ShoppingCart
        },
        {
            path: "/newsbook",
            name: 'newsbook',
            meta: {
                title: '新书'
            },
            component: BooksNew
        },
        {
            path: '/register',
            name: 'register',
            meta: {
                title: '注册页面'
            },
            component: UserRegister
        },
        {
            path: '/login',
            name: 'login',
            meta: {
                title: '登录页面'
            },
            component: UserLogin
        },
        {
            path: '/lineChart',
            name: 'lineChart',
            meta: {
                title: '折线图'
            },
            component: LineChart
        },
        {
            path: '/ceshi1',
            name: 'ceshi1',
            meta: {
                title: '折线图'
            },
            component: CeShi1
        },
        {
            path: '/ceshi2',
            name: 'ceshi2',
            meta: {
                title: '折线图'
            },
            component: CeShi2
        }
    ]
})


// router.beforeEach(async(to, from, next) => {
//     // NProgress.start()

//     // const isLogin = !!store.getters.token
//     const isLogin = false

//     if (isLogin) {
//         // await store.dispatch('info')

//         if (to.path === '/login') {
//             next({ path: '/' })
//                 // NProgress.done()
//         } else {
//             next()
//                 // NProgress.done()
//         }
//     } else {
//         if (to.path == '/login') {
//             next()
//         } else {
//             next({ path: '/login' })
//                 // NProgress.done()
//         }
//     }
// })

router.afterEach(to => {
    document.title = to.meta.title;
})

export default router